package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.entity.DeleteDTO;
import com.woniu.entity.TrainerDTO;
import com.woniu.mapper.TrainerMapper;
import com.woniu.pojo.Trainer;
import com.woniu.service.ITrainerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.PageData;
import com.woniu.util.Result;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 培训师 服务实现类
 * </p>
 *
 * @author xiaoruan
 * @since 2023-03-07
 */
@Service
public class TrainerServiceImpl extends ServiceImpl<TrainerMapper, Trainer> implements ITrainerService {
    @Resource
    TrainerMapper trainerMapper;

    @Override
    public PageData getPageData(Integer currentPage, Integer pageSize, TrainerDTO trainerDto) {
        // 1. 构建查询条件
        LambdaQueryWrapper<Trainer> queryWrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(trainerDto.getName())) {
            queryWrapper.like(Trainer::getName, trainerDto.getName());
        }

        if (!StringUtils.isEmpty(trainerDto.getLevel())) {
            queryWrapper.eq(Trainer::getLevel, trainerDto.getLevel());
        }

        if (!StringUtils.isEmpty(trainerDto.getCreateTimeStart())) {
            queryWrapper.ge(Trainer::getCreateTime, trainerDto.getCreateTimeStart());
        }

        if (!StringUtils.isEmpty(trainerDto.getCreateTimeEnd())) {
            queryWrapper.le(Trainer::getCreateTime, trainerDto.getCreateTimeEnd());
        }

        // 2. 创建分页对象
        // 3. 调用mapper查询数据 这边调用service
        Page<Trainer> page = new Page<>(currentPage, pageSize);

        Page<Trainer> trainerPage = this.page(page, queryWrapper);
        // 4. 封装返回的对象 pageData
        PageData pageData = new PageData(trainerPage.getTotal(), trainerPage.getRecords());
        // 5. 返回数据
        return pageData;
    }

    /**
     * 批量删除用sql写
     * 因为操作的是数组所以没有返回值
     * 而且加了逻辑删除则必须用update + 表名去写
     */
    @Override
    public void delSel(String[] ids) {
        trainerMapper.delSel(ids);

    }


    /**
     * 方式二,使用注解进行redis存储
     * */
    @Override
    @Cacheable(value = "trainer:all")
    public List<Trainer> findAll() {
        //或者 this.list()
        return trainerMapper.selectList(null);
    }


    @Override
    public Result delBatch(DeleteDTO deleteDTO) {
        try {
            int i = 0;
            Boolean j = true;

            for (String id : deleteDTO.getIds()) {
                i = trainerMapper.deleteById(id);
                if (i !=1) {
                    j = false;
                }
            }

            if (j) {
                return new Result(200, "删除成功");
            }

            return new Result(500, "删除失败");


        } catch (Exception e) {
            throw new RuntimeException("删除用户信息:" + e.toString());
        }

    }



}
